<template>
	<view>
		<view class="goods-list" >
			 <view v-for="(item, i) in list" :key="i" @click="gotoDetail(item)">
			    <!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
			    <my-goods :goods="item"></my-goods>
			  </view>
				<!-- 为 my-goods 组件动态绑定 goods 属性的值 -->
				
			<view v-if="finished" class="text">
				我也是有底线的
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj: {
					query: '',
					cid: null,
					pagenum: 0,
					pagesize: 10,
				},
				list: [],
				finished: false,
				loading: false //是否加载中

			};
		},
		onLoad(option) {
			if (option.cid) this.queryObj.cid = option.cid
			if (option.query) this.queryObj.query = option.query
			this.getGoodList()
		},
		methods: {
			async getGoodList() {
				this.loading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
				if (res.meta.status !== 200) {
					this.loading = false //错误时也要打开节流阀
					return uni.$showMsg()
				}
				// 数据处理
				// 追加数据
				this.list = [...this.list, ...res.message.goods]
				if (res.message.goods.length < this.queryObj.pagesize) {
					this.finished = true
				} else {
					// 让页码值自增 +1

					this.queryObj.pagenum++
					this.loading = false
				}
			},
			gotoDetail(item) {
			  uni.navigateTo({
			    url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
			  })
			}
		},
		// 触底的事件
		onReachBottom() {
			if (this.finished) return false
			if (this.loading) return false
			// 重新获取列表数据
			this.getGoodList()
		},
	  async	onPullDownRefresh() {
			  this.queryObj.pagenum = 1
			  this.list = []
			  this.finished=this.loading=false
			  
			 await this.getGoodList()
			// 关闭下拉
			 uni.stopPullDownRefresh()
			
		},
		
		
	}
</script>

<style lang="scss">
.text{
	color: #007AFF;
	padding-left: 270rpx;
	
}
</style>
